Thread: checking command line argument count [ac],linux

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    checking command line argument count [ac],linux

    Suppose the following two commands are acceptable

    Code:
    $copy hello.txt bye.txt
    $copy -f hello.txt bye.txt
    I would like to say this
    If user does NOT enter 3 or 4 arguments return error

    Code:
    if (ac != 3 || ac !=4)
     display error
    
    else {
      ...
    }
    When I do enter $copy hello.txt bye.txt or $copy -f hello.txt bye.txt , it sitll gives me error. This doesn't make sense, since I entered 3 OR 4 arguments

    HOWEVER, when I change the if statements to
    if (ac != 3 && ac !=4), the program goes onto the else statement

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why don't you actually printf argc to figure out how it works?

    Code:
    int main(int argc, char* argv[]) {
      printf("%d", argc);
      return 0;
    }
    Then test it with as many arguments as you would like and see what happens. Experimentation is the primary source for knowledge when it comes to programming languages.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by bos1234 View Post
    When I do enter $copy hello.txt bye.txt or $copy -f hello.txt bye.txt , it sitll gives me error. This doesn't make sense, since I entered 3 OR 4 arguments
    If it's 3 then it's NOT 4, if it's 4 then it's NOT 3.

    Try this?

    Code:
    if(argc < 3 || argc > 4)

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by Subsonics View Post
    If it's 3 then it's NOT 4, if it's 4 then it's NOT 3.

    Try this?

    Code:
    if(argc < 3 || argc > 4)
    The above logic works.
    Why doesn't my one work? Isn't it also correct?

    Code:
    if arguments are not equal to 3 or 4 
        display error
    but I have supplied 3 agruments

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by bos1234 View Post
    The above logic works.
    Why doesn't my one work? Isn't it also correct?
    No, accepted argument amount are 3 or 4. But if the amount IS 3 then it's not 4 and vice versa, it can not be both 3 AND 4 at the same time, it's either or.


    Quote Originally Posted by bos1234 View Post
    but I have supplied 3 agruments
    But you have not supplied 4.
    Last edited by Subsonics; 04-11-2012 at 04:37 AM.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    thanks for the explanation but i sitll dont get it

    I wrote
    if arguments supplied is NOT 3 OR 4
    display error

    $cp hello.txt bye.txt

    3 arguments supplied.
    argc != 3 will have (false), since argument supplied IS 3
    argc != 4 will have (true), since argument supplied IS NOT 4
    proceed

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by bos1234 View Post
    if arguments supplied is NOT 3 OR 4
    display error

    $cp hello.txt bye.txt

    3 arguments supplied.
    argc != 3 will have (false), since argument supplied IS 3
    argc != 4 will have (true), since argument supplied IS NOT 4
    proceed
    What you are saying is, if either of the tests fail, print an error message. Well, if argc is 3 then it's not 4 so that test will fail, and conversely if argc is 4 then it's not 3 so that test will fail.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    thanks man, i got it now

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Subsonics View Post
    Code:
    if arguments supplied is NOT 3 OR 4
    this should actually be
    Code:
    if arguments supplied is NOT ( 3 OR 4 )
    an this is the same as
    Code:
    if arguments supplied is ( NOT 3 ) AND ( NOT 4 )
    Kurt

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well yes, but I did not post that? I guess you could express it informally as if arguments supplied is not 3 nor 4.
    Last edited by Subsonics; 04-11-2012 at 05:07 AM.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Subsonics View Post
    Well yes, but I did not post that?
    No, you posted:
    Code:
    if (ac != 3 || ac !=4)
    And that in the middle is an OR, not an AND.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by QuantumPete View Post
    No, you posted:
    Code:
    if (ac != 3 || ac !=4)
    And that in the middle is an OR, not an AND.
    I'm confused, the quote is from bos1234 not me. What did I say where, can you make an actual quote?

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Subsonics View Post
    I'm confused, the quote is from bos1234 not me. What did I say where, can you make an actual quote?
    @QuantumPete: I just was not sure if you agreed or not. My position is:

    A | B | !A OR !B
    --+---+-----------
    1 | 1 | 0
    0 | 1 | 1
    1 | 0 | 1
    0 | 0 | 1

    That is, the test "if( argc != 3 or argc != 4 ) will only fail if argc is both 3 and 4 at the same time, the equivalent would be.

    A | B | !(A AND B)
    --+---+-----------
    1 | 1 | 0
    0 | 1 | 1
    1 | 0 | 1
    0 | 0 | 1

  14. #14
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    @Subsonics: Apologies, got confused with my quote. Yes you're right and so are those truth tables.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line argument
    By vaibhav in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2005, 10:38 AM
  2. command line argument help
    By bobnet in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2003, 12:22 AM
  3. command argument line
    By Tonyukuk in forum C++ Programming
    Replies: 5
    Last Post: 01-28-2003, 09:10 AM
  4. Command line argument
    By Max in forum C Programming
    Replies: 2
    Last Post: 07-17-2002, 02:10 PM
  5. '*' as a command line argument
    By kooma in forum C Programming
    Replies: 6
    Last Post: 02-26-2002, 02:18 PM